﻿using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWSEditor.CRM.Forms
{
    public partial class AutoDiscoverTestForm : Form
    {

        private ExchangeService _service;
        private AutodiscoverService _autoDiscoverService;
        private int redirectionCount = 0;
        public AutoDiscoverTestForm(ExchangeService Service)
        {
            InitializeComponent();
            _service = Service;
            txtDiscoveryUser.Text = CrmAuthInfoFactory.ExchangeCreds.UserName;
            chkEnableSCP.Checked = DetermineOrgType();
        }

        public ExchangeCredentials GetCredentials()
        {
            //ExchangeCredentials exchangeCreds = CrmAuthInfoFactory.ExchangeCreds;
            ExchangeCredentials exchangeCreds = _service.Credentials;
            return exchangeCreds;
        }

        private bool DetermineOrgType()
        {
            if (!string.IsNullOrEmpty(CrmAuthInfoFactory.CrmServiceUrl))
            {
                if (CrmAuthInfoFactory.CrmServiceUrl.Contains("dynamics.com"))
                {
                    chkEnableSCP.Enabled = true;
                    return false;
                }
                else
                {
                    chkEnableSCP.Enabled = true;
                    return true;
                }
            }
            return false; //Default to false if we haven't authenticated with Dynamics yet.
        }

        private void btnTestAccess_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(txtEmailAddress.Text)) || (!txtEmailAddress.Text.Contains("@")))
            {
                MessageBox.Show("Please enter a valid e-mail address.");
                return;
            }

            txtExternalEwsUrl.Text = "";
            txtInternalEwsUrl.Text = "";
            txtResults.Text = "";
            Cursor = Cursors.WaitCursor;
            CrmAutodiscoverService crmADservice = new CrmAutodiscoverService(this, _service, chkEnableSCP.Checked, numTimeout.Value, numMaxRedirects.Value, chkAllowNonSSL.Checked, txtEmailAddress.Text);
            crmADservice.InitializeService();
            crmADservice.PerformAutoDiscover();
            Cursor = Cursors.Default;

        }

        public void LogError(Exception ex)
        {
            StringBuilder sb = new StringBuilder(txtResults.Text);
            sb.AppendLine(DateTime.Now.ToString());
            sb.AppendLine("Level: " + TraceLevel.Error);
            sb.AppendLine(ex.Message);
            //sb.AppendLine(ex.InnerException.HResult.ToString());
            sb.AppendLine(ex.StackTrace);
            txtResults.Text = sb.ToString();
        }

        public void LogTrace(string Info, TraceLevel traceLevel)
        {
            StringBuilder sb = new StringBuilder(txtResults.Text);
            sb.AppendLine(DateTime.Now.ToString());
            sb.AppendLine("Level: " + traceLevel.ToString());
            sb.AppendLine(Info);
            txtResults.Text = sb.ToString();

            if (traceLevel == TraceLevel.Error)
            {
                tslblStatus.Text = Info;
                tslblStatus.Image = EWSEditor.Properties.Resources.error;
            }

        }

        private void lblEmailAddress_Click(object sender, EventArgs e)
        {

        }

        private void txtEmailAddress_TextChanged(object sender, EventArgs e)
        {

        }

        private void picDiscoveryInfo_MouseHover(object sender, EventArgs e)
        {
            ToolTip tip = new ToolTip();
            tip.SetToolTip(this.picDiscoveryInfo, "The Discovery Service User is derived from the currently configured Exchange Service account.");
        }
    }

    public enum TraceLevel
    {
        Info = 0,
        Warning = 1,
        Error = 2
    }
}
